/*
 * i2c_eeprom.h
 *
 *  Created on: 04.04.2014
 *      Author: alexs
 */

#ifndef I2C_EEPROM_H_
#define I2C_EEPROM_H_

#include "main.h"
#include <util/twi.h>

#define TWI_wait()  while(!(TWCR&(1<<TWINT)));TWI_last_error=TW_STATUS
#define TWI_start()  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWSTO);TWI_wait()
#define TWI_restart()  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);TWI_wait()
#define TWI_stop()  TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN)
#define TWI_send_data_nack(a)   TWDR=a;TWCR=(1<<TWINT)|(1<<TWEN);TWI_wait()
#define TWI_send_data_ack(a)   TWDR=a;TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);TWI_wait()
#define TWI_check(a) (TW_STATUS==a?0:1)
#define I2C_EEPROM 0xa0
#define ifTWI(a,b) if (TWI_check(a)) {TWI_stop();TWCR=0;return b;}


uint8_t i2c_ee_read_tag(const uint16_t addr,uint8_t * buff);
uint8_t i2c_ee_write_tag(const uint16_t addr,uint8_t * buff,const uint8_t len);
void i2c_ee_clear_tags(uint8_t * local);

#endif /* I2C_EEPROM_H_ */
